package com.mc13.gpslogger;

import java.util.Calendar;

import android.location.Location;

public class GPSData {
	// easy life: no getters, setters
	public double latitude;
	public double longitude;
	public double altitude;
	public float accuracy;
	public float speed;
	public float bearing;
	public long timestamp;
	
	public GPSData(Location location) {
		this.timestamp = Calendar.getInstance().getTimeInMillis();
		initialize(location);
	}
	
	public GPSData(Location location, long timestamp) {
		this.timestamp = timestamp;
		initialize(location);
	}
	
	private void initialize(Location location) {
		latitude = location.getLatitude();
		longitude = location.getLongitude();
		altitude = location.getAltitude();
		accuracy = location.getAccuracy();	
		bearing = location.getBearing();
		speed = location.getSpeed();
	}
	
	public boolean isCloseTo(GPSData otherLocation, double threshold) {
		if ((Math.abs(latitude - otherLocation.latitude) < threshold) &&
				(Math.abs(longitude - otherLocation.longitude) < threshold)) {
			return true;
		} else return false;
	}
	
	public String toCSVrow() {
		String _sep = ";";
		return str(timestamp) + _sep + str(latitude) + _sep + str(longitude) + _sep + str(altitude) + 
			str(bearing) + _sep + str(speed)+ _sep + str(accuracy);
	}
	
	public String toLogField() {
		String _sep = "\n";
		return "Timestamp: " + str(timestamp) + "ms" + _sep + 
				"Latitude: " + str(latitude) + _sep +
				"Longitude: " + str(longitude) + _sep +
				"Altitude: " + str(altitude) + _sep +
				"Bearing: " + str(bearing) + _sep +
				"Speed: " + str(speed) + _sep +
				"Accuracy: " + str(accuracy);
	}
	
	static public String str(Object value) {
		return String.valueOf(value);
	}
}
